import { Component, OnInit, OnDestroy } from '@angular/core'
import { INewPageColumn } from '@shared/interfaces/common'
import { ComWindow } from '@shared/classes/ComWindowClass'
import { TabBaseComponent } from './tab-base/tab-base.component'
import { TabEditRulesComponent } from './tab-edit-rules/tab-edit-rules.component'
import { TabMerchantComponent } from './tab-merchant/tab-merchant.component'
import { PostRiskStoreService } from './post-risk-store.service'
import { NzMessageService } from 'ng-zorro-antd'
import { I18NService } from '@core'
import { PostRiskService } from '../post-risk.service'
import { PageService } from '@shared/services/page.service'
import { Router, ActivatedRoute } from '@angular/router'
import { Institution } from '@shared/interfaces/post-risk-control'
import { SuccessCode, ErrorCode } from '@shared/interfaces/common'

@Component({
  selector: 'app-new',
  templateUrl: './new.component.html',
  styles: [],
})
export class NewComponent implements OnInit, OnDestroy {
  pages: INewPageColumn[] = []
  type: number
  constructor(
    private riskStore: PostRiskStoreService,
    public msgSrv: NzMessageService,
    private i18n: I18NService,
    private riskService: PostRiskService,
    public pageService: PageService,
    public router: Router,
    public route: ActivatedRoute,
  ) {}

  ngOnInit() {
    this.initPages()
  }

  ngOnDestroy() {
    this.riskStore.reset()
  }

  initPages(): void {
    this.pages = [
      {
        component: new ComWindow(TabBaseComponent, { title: 'GENERAL.INFO.OTHER.BASIC_CONFIG' }),
      },
      {
        component: new ComWindow(TabEditRulesComponent, { title: 'RISK_CTRL.RULES' }),
      },
      {
        component: new ComWindow(TabMerchantComponent, { title: 'GENERAL.INFO.RANGE' }),
      },
    ]
  }

  submit(): void {
    const riskScheme = this.riskStore.value
    delete riskScheme.ruleLists
    riskScheme.insCode = (riskScheme.insCode as Institution).code
    this.route.queryParams.subscribe(params => {
      this.type = Number(params.type)
    })
    if (this.type === 2) {
      // 编辑方案
      const { schemeCode, storeInfoList } = riskScheme
      riskScheme.intStoreCodeList = []
      if (storeInfoList) {
        storeInfoList.forEach(element => {
          riskScheme.intStoreCodeList.push(element.code)
        })
      }
      delete riskScheme.storeInfoList
      delete riskScheme.mcccInfoList
      const postriskSchemes = JSON.stringify(riskScheme)
      this.riskService.update(schemeCode, postriskSchemes).subscribe(() => {
        this.router.navigate(['postRisk/list'])
      })
      return
    }
    if (this.type === 1) {
      // 复制方案
      riskScheme.schemeCode = null
      delete riskScheme.mcccInfoList
    }
    const riskSchemes = JSON.stringify(riskScheme)
    this.riskService.create(riskSchemes).subscribe(() => {
      /** 成功后不需要路由守卫 */
      this.pageService.triggerGuardAlive(false)
      this.router.navigate(['postRisk/list'])
    })
  }

  trans(key: string, params?: Object): string {
    return this.i18n.fanyi(key, params)
  }
}
